package com.lbssoft.gpsbd.controller;

import com.lbssoft.gpsbd.annotation.LogAnnotation;
import com.lbssoft.gpsbd.model.DeviceType;
import com.lbssoft.gpsbd.model.Result;
import com.lbssoft.gpsbd.model.vo.UserVo;
import com.lbssoft.gpsbd.service.DeviceTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

@Api("设备类型")
@RestController
@RequestMapping("/devicetypes")
public class DeviceTypeController {


	@Autowired
	private DeviceTypeService deviceTypeService;

	@ApiOperation(value="类型列表", notes="类型列表")
	@RequiresPermissions(value = {"system:devicetype:view", "user:common:devicetype"}, logical = Logical.OR)
	@RequestMapping(value = "",method = {RequestMethod.GET})
	@ResponseBody
	public Result list() {
		UserVo user = (UserVo) SecurityUtils.getSubject().getPrincipal();
		String where = "";
		if(user.getGroupId()==0){
			where =where+"where disabled=false";
		}
		List<DeviceType> types = deviceTypeService.list(where);
		Result result = new Result();
		result.setCode(0);
		result.setData(types);
		return result;
	}

	@ApiOperation(value="添加类型", notes="添加类型")
	@LogAnnotation(targetType = "type",action = "add",remark = "添加设备类型")
	@RequiresPermissions("system:devicetype:add")
	@RequestMapping(value = "",method = {RequestMethod.POST})
	@ResponseBody
	public Result add(@Validated @RequestBody DeviceType type) {
		deviceTypeService.add(type);
		return new Result();
	}

	@ApiOperation(value="删除类型", notes="删除类型")
	@LogAnnotation(targetType = "type",action = "delete",remark = "删除设备类型")
	@RequiresPermissions("system:devicetype:delete")
	@RequestMapping(value = "/{id}",method = {RequestMethod.DELETE})
	@ResponseBody
	public Result delete(@NotNull @PathVariable int id) {
		deviceTypeService.delete(id);
		return new Result();
	}

	@ApiOperation(value="修改类型", notes="修改设备类型")
	@ApiImplicitParam(paramType="body",name = "type", value = "类型信息", dataType = "DeviceType")
	@LogAnnotation(targetType = "type",action = "update",remark = "修改设备类型")
	@RequiresPermissions("system:devicetype:update")
	@RequestMapping(value = "",method = {RequestMethod.PUT})
	@ResponseBody
	public Result update(@Validated @RequestBody DeviceType type) {
		Result result = new Result();
		deviceTypeService.update(type);
		return result;
	}

	
}